package Bai08;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.awt.FlowLayout;

@SuppressWarnings("serial")
public class Canvas extends JFrame {
	DatagramSocket socket;
	DatagramPacket dp;
	InetAddress ia;
	private JPanel contentPane;
	private float size=3.0f;
	private Color color=Color.BLUE;
	private Graphics2D g2d;
	private Point StartP=new Point();
	private Point EndP=new Point();
	JComboBox<String> comboBox = new JComboBox<String>();
	JComboBox<String> comboBox_1 = new JComboBox<String>();
	JPanel panel_2 = new JPanel();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Canvas frame = new Canvas();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Canvas() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 458, 294);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBounds(5, 5, 145, 82);
		
	
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str=(String)comboBox.getSelectedItem();
				System.out.println(str);
				switch(str){
				case "Red":color=Color.RED;break;
				case "Green":color=Color.GREEN;break;
				case "Blue":color=Color.BLUE;break;
				case "Yellow":color=Color.YELLOW;break;
				case "Orange":color=Color.ORANGE;break;
				case "Pink":color=Color.PINK;break;
				case "Cyan":color=Color.CYAN;break;
				case "Black":color=Color.BLACK;break;
				
				}
			}
		});
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Red", "Green", "Blue", "Cyan", "Black", "Yellow", "Orange", "Pink"}));
		comboBox.setSelectedIndex(1);
		panel.add(comboBox);
		panel_2.setBounds(160, 5, 286, 252);
		panel_2.setBackground(Color.WHITE);
		
		
		panel_2.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				EndP=e.getPoint();
				g2d=(Graphics2D) panel_2.getGraphics();
				g2d.setStroke(new BasicStroke(size));
				g2d.setColor(color);
				Objective obj=new Objective(StartP, EndP, color, size);
				writeObjectToSocket(obj);
				g2d.drawLine(StartP.x, StartP.y, EndP.x, EndP.y);
				StartP=EndP;
			}
		});
		panel_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				g2d=(Graphics2D) panel_2.getGraphics();
				g2d.setStroke(new BasicStroke(size));
				g2d.setColor(color);
				StartP=e.getPoint();
				EndP=e.getPoint();
				Objective obj=new Objective(StartP, EndP, color, size);
				writeObjectToSocket(obj);	
				g2d.drawLine(StartP.x, StartP.y, EndP.x, EndP.y);
			}
		});
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		contentPane.setLayout(null);
		contentPane.add(panel);
		panel.add(comboBox_1);
		
		
		comboBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str=(String)comboBox_1.getSelectedItem();
				System.out.println(str);
				size=Float.parseFloat(str);
			}
		});
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "6", "8", "10", "20", "30"}));
		comboBox_1.setSelectedIndex(1);
		contentPane.add(panel_2);
		socketInit();
	}
	public void socketInit(){
		try {
			socket = new DatagramSocket();
			ia = InetAddress.getLocalHost();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}		
	}
	private void writeObjectToSocket(Objective obj) {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		try {
			ObjectOutputStream os = new ObjectOutputStream(byteStream);
			os.writeObject(obj);
			dp = new DatagramPacket(byteStream.toByteArray(), byteStream.size(), ia, 2048);
			socket.send(dp);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	}
}
